import { mapState } from 'vuex'

module.exports = {
	
	data() {
		return {
			loginFunName: '',
			loginFunParams: undefined,
		}
	},
	computed: {
		...mapState(['vuex_config', 'vuex_business', 'vuex_user', 'vuex_address', 'vuex_hotel_search',
			'vuex_system', 'vuex_adminuser', 'vuex_businessObj','vuex_meal_list','park_area_list','my_community','proShopPayList','vuex_router_query'
		]),
		
		formatPictureImg(data) {
			let $this = this
			let img;
			return function(data) {
				img = !data ? $this.vuex_config.errImg : data.split(',')[0]
				return img
			}
		},
	},
	beforeCreate() {
		this.$u.vuex = (name, value) => {
			this.$store.commit('$uStore', {
				name,
				value
			})
		}
	},

	onUnload() {
		uni.$off('loginSuccess')
		uni.$off('loginFail')
		// #ifdef MP-ALIPAY	
		this.$u.vuex('vuex_router_query',{})
		// #endif
	},
	onHide() {
		uni.$off('loginSuccess')
		uni.$off('loginFail')
	},
	methods: {

		/**
		 * 检测是否已登录
		 * @returns {boolean}
		 */
		$isLogin() {
			try {
				return !!this.$store.state.vuex_token
			} catch (e) {
				return false
			}
		},
		/**
		 * 检测是否登录，若未登录则打开登录弹框
		 * @param funName 登录后回调的函数名称
		 * @param params 登录后回调的函数的参数
		 * @param failName 选择暂不登录的回调  
		 * @returns {Promise<unknown>}
		 */
		$login(funName = '', params = null, failName = null) {
			return new Promise((resolve, reject) => {
				if (this.$isLogin()) {
					resolve()
				} else {
					this.loginFunName = funName
					this.loginFunParams = params
					this.failName = failName
					try {
						this.$u.vuex('vuex_login', true)
						uni.$once('loginSuccess', this.$loginSuccess) // 打开了登录弹窗，如果暂不登录记得销毁监听
						uni.$once('loginFail', this.$loginFail) // 打开了登录弹窗，如果确认要登录，记得销毁监听
					} catch (e) {
						console.log(e);
					}
					reject()
				}
			})
		},
		/**
		 * 登录成功的回调
		 */
		$loginSuccess() {
			if (this.loginFunName) {
				this[this.loginFunName](this.loginFunParams)
			}
		},
		/**
		 * 登录失败的回调
		 */
		$loginFail() {
			if (this.failName) {
				this[this.failName]()
			}
		},
		/**
		 * 验证登录后跳转路由
		 * @param url
		 */
		$to(url) {
			this.$login('$to', url).then(() => {
				this.$linkTo(url)
			}).catch(() => {})
		},

		$toProd(item) {
			let url = ''
			if (item.prodType == 1) {
				url = `/package/product/local/prod/index?id=${item.pk_id}`
			} else if (item.prodType == 3) {
				url = `/package/hotel/reserve/details/index?id=${item.pk_id}`
			} else if (item.prodType == 4) {
				url = `/package/line/detail/index?id=${item.pk_id}`
			} else if (item.prodType == 6) {
				url = `/package/product/vipProd/detail?id=${item.pk_id}`
			} else if (item.prodType == 9) {
				url = `/package/product/ticket/prod/index?id=${item.pk_id}`
			} else if (item.prodType == 10) {
				url = `/package/product/group/details/index?id=${item.pk_id}`
			} else if (item.prodType == 11) {
				url = `/package/product/bonusProd/detail?id=${item.pk_id}`
			}else if(item.prodType == 14){
				url = `/package/product/dutchAuction/index?id=${item.pk_id}`
			}
			// #ifndef  MP-ALIPAY
			let pages = getCurrentPages()
			let index = pages.findIndex(item => url.includes(item.route))
			if (index > -1) {
				pages[index].onLoad({
					id: item.pk_id
				})
				uni.navigateBack({
					delta: pages.length - 1 - index
				})
			} else {
				// this.$to(url)
				uni.navigateTo({
					url
				})
			}
			// #endif
			
			// #ifdef   MP-ALIPAY
				// this.$to(url)
				uni.navigateTo({
					url
				})
			// #endif
			
		},
		/**
		 * 获取节点信息
		 * @param selector
		 * @param all 是否匹配所有节点
		 * @param params 
		 * @returns {Promise<unknown>}
		 */
		$getFields(selector, all = false, params = {
			size: true
		}) {
			return new Promise(resolve => {
				uni.createSelectorQuery().
				in(this)[all ? 'selectAll' : 'select'](selector)
					.fields(params, rect => {
						if (all && Array.isArray(rect) && rect.length) {
							resolve(rect)
						}
						if (!all && rect) {
							resolve(rect)
						}
					}).exec()
			})
		}
	}
}
